package com.yzy.fly.entity.role;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.yzy.fly.kernel.model.ability.tree.Tree;
import com.yzy.fly.util.DateUtil;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

@Data
@TableName("fly_model_data")
public class FlyModelData implements Serializable,Tree<Integer,FlyModelData> {
    
    @TableId(type = IdType.AUTO)
    private Integer id;
    
    private String name;

    private String globalKey;

    private String type;

    private Integer fieldOriginId;

    private Integer fieldId;

    private String fieldName;

    private String classFieldName;

    private String isWrapper;

    private String wrapperClass;

    private String classPath;

    private String isArray;

    private String tableName;

    private Integer parentId;

    private Long businessId;

    private Integer parentFieldId;

    private String parentFieldName;

    private Integer systemId;

    @DateTimeFormat(pattern= DateUtil.DateFormatPattern.yyyy_MM_dd_HH_mm_ss)
    @JsonFormat( pattern = DateUtil.DateFormatPattern.yyyy_MM_dd_HH_mm_ss)
    private Date createTime;
    @DateTimeFormat(pattern= DateUtil.DateFormatPattern.yyyy_MM_dd_HH_mm_ss)
    @JsonFormat( pattern = DateUtil.DateFormatPattern.yyyy_MM_dd_HH_mm_ss)
    private Date updateTime;
    @TableField(exist=false)
    private boolean open=true;
    @TableField(exist=false)
    private List<FlyModelData> childrens;
    @TableField(exist=false)
    private List<FlyAttribute> attrs;
    /**
     * 方法说明:获取实体类名称
     */
    public String getEntityClazz() {
        int index = classPath.lastIndexOf('.');
        return classPath.substring(index + 1);
    }

    @Override
    public boolean isRootNode() {
        if (parentId == null || parentId.intValue() == 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean isParentNode(Integer parentId) {
        if (parentId != null && id != null &&  parentId.intValue() == id.intValue()) {
            return true;
        }
        return false;
    }

    @Override
    public boolean isChildNode(FlyModelData parent) {
        if(parentId.intValue() == parent.getId().intValue()) {
            return true;
        }
        return false;
    }
}
